# 变量labels自动识别机制
# 多个主流R包（如gt、Hmisc、labelled等）长期采用非正式约定，
# 用变量的label属性存储人类可读的标签。
# 如今ggplot2正式加入这一生态，自动将变量的label属性作为默认图形标签使用。
# 这意味着：若数据框的某列包含label属性（如：attr(df$age, "label") <- "患者年龄"；
# 绘图时无需手动指定，ggplot2会自动将其作为轴标题或图例标题。

df <- penguins
# Manually set label attributes.
# Other packages may offer better tooling than this.
attr(df$species, "label") <- "Penguin Species"
attr(df$bill_dep, "label") <- "Bill Depth (mm)"
attr(df$bill_len, "label") <- "Bill Length (mm)"
attr(df$body_mass, "label") <- "Body Mass (g)"

ggplot(df, aes(bill_dep, bill_len, colour = species)) +
  geom_point(na.rm = TRUE)

# 新增labs(dictionary)参数极大地提升了为变量设置标签的效率和准确性，
# 通过setNames() or dplyr::pull()设置。
dict <- tibble::tribble(
  ~variable, ~label,
  "species", "Penguin Species",
  "bill_dep", "Bill Depth (mm)",
  "bill_len", "Bill Length (mm)",
  "body_mass", "Body Mass (g)"
)

ggplot(df, aes(bill_dep, bill_len, colour = species)) +
  geom_point(na.rm = TRUE) +
  labs(dictionary = dict)
  # Or:
  # labs(dictionary = dplyr::pull(dict, label, name = var) #通过plyr::pull设置

  